package com.hongshan.wallet

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.support.v7.app.AlertDialog
import android.text.format.DateFormat
import android.widget.Toast
import com.elvishew.xlog.XLog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.UnsupportedEncodingException
import java.math.BigDecimal
import java.net.URLDecoder
import java.net.URLEncoder
import java.text.DecimalFormat
import java.util.*

/**
 * Copyright Qiandai Ltd.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet
 * @author   : Administrator
 * @data     : 2019/3/18
 * @email    : zhangxx@baihang-china.com
 */

fun Any.log(msg:Any? = this.toString()){
    XLog.d(msg?:"null")
}

fun Double.makeString(regex: String = "#.##"): String = DecimalFormat(regex).format(this)

fun Double.toNoZeroString(): String {
    val s = BigDecimal.valueOf(this).toString()
    if (!s.contains(".")) {
        return s
    }
    for (i in s.length downTo 1) {
        if (s[i - 1] == '.') {
            return s.substring(0, i - 1)
        } else if (s[i - 1] != '0') {
            return s.substring(0, i)
        }
    }
    return ""
}

fun Long.parseTime(format: String = "yyyy-MM-dd HH:mm") = DateFormat.format(format, Date(this * 1000)).toString()

fun <T> List<T>.containsField(block: (T) -> Boolean): Boolean {
    forEach { if (block.invoke(it)) return true }
    return false
}

fun <T>tryRun(block:()->T): T? {
    return try {
        block()
    } catch (e:Throwable){
        e.printStackTrace()
        null
    }
}

fun toast(msg:String?){
    if (msg == null) return
    GlobalScope.launch(Dispatchers.Main) {
        Toast.makeText(MyApplication.applicationContext,msg,Toast.LENGTH_LONG).show()
    }
}

fun Date.format(fmr: String = "yyyy-MM-dd") = DateFormat.format(fmr, this).toString()
fun Long.format(fmr: String = "yyyy-MM-dd") = DateFormat.format(fmr, this * 1000).toString()

fun String.makeSalt(): String {
    val saltLength = length / 2
    val buffer = StringBuffer()
    if (saltLength != 0) {
        for (i in 0 until  saltLength) {
            buffer.append("*")
        }
        val otherLength = length - saltLength
        val leftlength = otherLength / 2
        val rightLength = otherLength - leftlength
            val rightStartIndex = leftlength + buffer.length
        val rightText = if (rightStartIndex < length) substring(rightStartIndex, length) else ""
        return substring(0, leftlength) + buffer + rightText
    }
    return this
}

fun copyStr(text:String?) {
    val cm = MyApplication.applicationContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    val mClipData = ClipData.newPlainText("Label", text)
    cm.primaryClip = mClipData
    toast("复制成功")
}


fun urlEncode(url: String): String? {
    return try {
        URLEncoder.encode(url, "UTF-8")
    } catch (e: UnsupportedEncodingException) {
        e.printStackTrace()
        null
    }
}

fun urlDecode(url: String): String? {
    return try {
        var prevURL = ""
        var decodeURL = url
        while (prevURL != decodeURL) {
            prevURL = decodeURL
            decodeURL = URLDecoder.decode(decodeURL, "UTF-8")
        }
        decodeURL
    } catch (e: UnsupportedEncodingException) {
        e.printStackTrace()
        null
    }
}

fun Context.showAlert(title: String? = null, message: String? = null, buttonText: String? = null) {
    AlertDialog.Builder(this)
        .setTitle(title)
        .setMessage(message)
        .setPositiveButton(buttonText) { dialog, _ ->
            dialog.dismiss()
        }
        .create()
        .show()
}